//
//  BaseViewController.m
//  ChatForHot
//
//  Created by FLY on 2018/10/8.
//  Copyright © 2018年 FLY. All rights reserved.
//

#import "BaseViewController.h"
#import "LoadingView.h"
#import "AppDelegate.h"
@interface BaseViewController ()<UIGestureRecognizerDelegate>

@property (nonatomic,strong) LoadingView *loadingView;

@end

@implementation BaseViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        [self HOTCHATdidInitialized];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        [self HOTCHATdidInitialized];
    }
    return self;
}

- (void)HOTCHATdidInitialized {
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.hidesBottomBarWhenPushed = YES;
}

/**显示menu*/
- (void)HOTCHATshowMenu{
    AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [appdelegate.TabbarMenu HOTCHATshowMenuView];
}
/**隐藏menu*/
- (void)HOTCHAThiddenMenu{
    AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [appdelegate.TabbarMenu HOTCHAThiddenMenuView];
}
- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [self HOTCHATsetNavigationItemsIsInEditMode:NO animated:NO];
    [self.navigationController setNavigationBarHidden:self.isHiddenNav animated:animated];
    if (self.navigationController.viewControllers.count > 1) {
        [self HOTCHAThiddenMenu];
    }else{
        [self HOTCHATshowMenu];
    }
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
}

- (void)HOTCHATsetNavigationItemsIsInEditMode:(BOOL)isInEditMode animated:(BOOL)animated{}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isHiddenNav = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor  whiteColor];
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forBarMetrics:(UIBarMetricsDefault)];
    if (self.navigationController.viewControllers.count >1) {
        
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"backBtnImg"] style:UIBarButtonItemStylePlain target:self action:@selector(HOTCHATbackClcik)];
    }
    
    [self HOTCHATInsertSubViews];
}
- (void)HOTCHATInsertSubViews{
    
    
}

- (CGFloat)navigationBarMaxYInViewCoordinator {
    if (!self.isViewLoaded) {
        return 0;
    }
    if (!self.navigationController.navigationBar || self.navigationController.navigationBarHidden) {
        return 0;
    }
    CGFloat result = 44.0;
    if (![UIApplication sharedApplication].statusBarHidden) {
        if (iPhoneX || iPhoneXR || iPhoneXSMAX) {
            result += 44.0;
        }else{
            result += 20.0;
        }
    }
    return result;
}

- (CGFloat)toolbarSpacingInViewCoordinator {
    if (!self.isViewLoaded) {
        return 0;
    }
    if (!self.navigationController.toolbar || self.navigationController.toolbarHidden) {
        return 0;
    }
    CGRect toolbarFrame = CGRectIntersection(self.view.bounds, [self.view convertRect:self.navigationController.toolbar.frame fromView:self.navigationController.toolbar.superview]);
    CGFloat result = CGRectGetHeight(self.view.bounds) - CGRectGetMinY(toolbarFrame);
    return result;
}

- (CGFloat)tabBarSpacingInViewCoordinator {
    if (iPhoneX || iPhoneXR || iPhoneXSMAX) {
        return 83.0;
    }else{
        return 49.0;
    }
}

//loading
- (void)HOTCHATstartLoading{
    
    [self HOTCHATstartLoadingInView:self.view];
}
- (void)HOTCHATstartLoadingInView:(UIView *)view{
    
    [view addSubview:self.loadingView];
    [self.loadingView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(view);
    }];
    [self.loadingView HOTCHATstartAni];
}
- (void)HOTCHATstopLoading{
    
    [self.loadingView HOTCHATstopAni];
    [self.loadingView removeFromSuperview];
}
//backaction
- (void)HOTCHATbackClcik{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}
//showmessage
- (void)HOTCHATshowMessage:(NSString *)message{
    
    UIView *windowview = [[UIApplication sharedApplication].delegate window];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:windowview animated:YES];
    hud.userInteractionEnabled = NO;
    hud.color = [UIColor colorWithWhite:0 alpha:0.7];
    // Configure for text only and offset down
    hud.mode = MBProgressHUDModeText;
    hud.labelText = message;
    hud.margin = 14.f;
    hud.removeFromSuperViewOnHide = YES;
    hud.yOffset = (HOTCHATUIHEIGHT / 2 - 90);
    [hud hide:YES afterDelay:2.3];
}
//lazzy
- (LoadingView *)loadingView{
    
    if (_loadingView == nil) {
        
        _loadingView = [[LoadingView alloc]init];
    }
    return _loadingView;
}
@end
